﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NPipeline.Stage
{
    public class AddToCollectionStage<T> : BaseStage
    {
        private List<T> collection;
        // this needs to be fixed ...
        private bool syncronized;

        public List<T> Collection { get { return collection; } }

        public AddToCollectionStage() 
        {
            this.collection = new List<T>();       
        }

        public AddToCollectionStage(IEnumerable<T> collection)
            : this(collection, true)
        {
        }

        public AddToCollectionStage(IEnumerable<T> collection, bool syncronized)
            : this()
        {
            this.collection.AddRange(collection);
            this.syncronized = syncronized;
        }

        /// <summary>
        /// If the obj received by stage equals to T, it is added to the collection
        /// and passed on to the next stage
        /// Object not matching the type are ignored
        /// </summary>
        /// <param name="obj"></param>
        public override void Process(object obj)
        {
            if (obj is T)
            {
                lock (this)
                {
                    collection.Add((T)obj);    
                }
                
            }
            // emit to next stage
            Emit(obj);
        }


    }
}
